package com.atguigu.flow.utils;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public
class BeanTomap {
    public static<T> Map<String,Object> replaceContext(Class<?> clazz, T obj,HashMap<String, Object> mapfield ) throws  Exception {
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> mapComparison=getProxyPojoValue(clazz);
        //System.out.println("mapComparison"+mapComparison);
        //System.out.println(mapfield);
        for (Map.Entry<String,Object> entry: mapComparison.entrySet()) {
            //System.out.println(entry);
            if (mapfield.containsKey(entry.getKey())) {
                System.out.println(entry);
                Field f = clazz.getDeclaredField(entry.getValue().toString());

                try {
                    //打开可见性，要不然获取不到值
                    f.setAccessible(true);

                    map.put(f.getName(), f.get(obj));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } finally {
                    //关闭可见性
                    f.setAccessible(false);

                }
            }
        }
        return map;
    }

    public static HashMap getProxyPojoValue(Class<?> clazz){
        String id = null;
        // 返回参数
        HashMap<String,Object> hashMap = new HashMap<>(16);
        //for (String s : key1) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            //System.out.println(field);

            // 获取表名
//                TableName table = object.getClass().getAnnotation(TableName.class);
//                if (table != null) {
//                    String tableName = table.value();
//                    hashMap.putIfAbsent("tableName", tableName);
//                }
            // 获取主键id
            if (id == null) {
                boolean isIdField = field.isAnnotationPresent(TableId.class);
                if (isIdField) {
                    TableField tableField = field.getAnnotation(TableField.class);
                    //if (s.toLowerCase().equals(field.getName().toLowerCase())) {
                    //System.out.println(field.getName());
                    String tableId = tableField.value();
                    System.out.println(tableId);
                    tableId = tableId.replaceAll("(?:\\[|null|\\]| +)", "");

                    hashMap.put(tableId,field.getName());
                    id = tableId;
                }
                //}
            }

            // 获取字段的值
            boolean isTableField = field.isAnnotationPresent(TableField.class);
            if (isTableField) {
                TableField tableField = field.getAnnotation(TableField.class);
                //if (s.toLowerCase().equals(field.getName().toLowerCase())) {
                String fieldValue = tableField.value();
                fieldValue = fieldValue.replaceAll("(?:\\[|null|\\]| +)", "");
                //System.out.println(fieldValue);
                hashMap.put(fieldValue,field.getName());
                //}
            }
        }
        //}
        return(hashMap);
    }

}
